import requests
import plotly.express as px
from operator import itemgetter

url = 'https://api.github.com/search/repositories?q=language:python+sort:stars+stars:>100000'

headers = {"Accept":'application/vnd.github.v3+json'}
resp = requests.get(url, headers=headers)
print(resp.text)

resp_dicts = resp.json()

items = resp_dicts['items']
print(f'返回仓库数：{len(items)}')

# 排序调用
items = sorted(items, key=itemgetter('stargazers_count'),reverse=False)

print('选择的最受欢迎的库有：')
links,stars,descps = [],[],[]
for item in items:
    links.append(f"<a href='{item['html_url']}'>{item['name']}</a>")
    stars.append(item['stargazers_count'])
    descps.append(item['description'])
    print(f'名称：{item["name"]}')
    print(f'所有者：{item["owner"]["login"]}')
    print(f'star数：{item["stargazers_count"]}')
    print(f'仓库地址：{item["html_url"]}')
    print(f'描述：{item["description"]}\n')

fig = px.bar(x=links,y=stars,
             width=1000,height=800,
             title="最受欢迎的库", labels={'x':'仓库','y':'星数'},
             hover_name=descps
            )
fig.update_layout(title_font_size=28, xaxis_title_font_size=20, yaxis_title_font_size=20)
fig.update_traces(marker_color='SteelBlue', marker_opacity=0.6)
fig.show()

